﻿using System;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;

namespace GUI.Controls.News
{
    public partial class HighlightNewsAtListPage : System.Web.UI.UserControl
    {
        private const string TopItemFormat = "{4}<h2><a href=\"{0}\" title=\"{2}\">{1}{5}</a></h1>{3}";
        private const string LiFormat = "<li>{3}<h2><a href=\"{0}\" title=\"{2}\">{1}{4}</a></h2></li>";
        private const int ImageWidth1 = 428;
        private const int ImageHeight1 = 320;
        private const int ImageWidth2 = 215;
        private const int ImageHeight2 = 148;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        internal bool DataBind(System.Data.DataTable dt)
        {

            if (dt != null && dt.Rows.Count > 0)
            {
                #region TopItem
                {
                    var r = dt.Rows[0];
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth1, ImageHeight1);

                    ltrTopItem.Text = string.Format(
                        TopItemFormat,
                        r[SqlConstants.NewsPublish.Url],
                        r[SqlConstants.NewsPublish.Title],
                        r[SqlConstants.NewsPublish.EncodedTitle],
                        string.IsNullOrEmpty(r[SqlConstants.NewsPublish.InitSapo].ToString()) ? string.Empty : string.Format("<p>{0}</p>", r[SqlConstants.NewsPublish.InitSapo]),
                        avatar,
                        r[SqlConstants.NewsPublish.NewsTypeIcon]
                        );
                }
                #endregion

                #region OtherItems
                {
                    if (dt.Rows.Count > 1)
                    {
                        var sb = new System.Text.StringBuilder();
                        for (var i = 1; i < dt.Rows.Count; i++)
                        {
                            var r = dt.Rows[i];
                            var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth2, ImageHeight2);

                            sb.AppendFormat(
                                LiFormat,
                                r[SqlConstants.NewsPublish.Url],
                                r[SqlConstants.NewsPublish.Title],
                                r[SqlConstants.NewsPublish.EncodedTitle],
                                avatar,
                                r[SqlConstants.NewsPublish.NewsTypeIcon]);
                        }
                        ltrOtherItem.Text = sb.ToString();
                    }
                }
                #endregion

                return true;
            }

            return false;
        }

        internal bool DataBind(Utilities.Entities.SearchResultItem[] data)
        {
            if (data != null && data.Length > 0)
            {
                #region TopItem
                {
                    var r = data[0];
                    var avatar = PresentationUtils.Photo.BuildPhoto(r.Avatar, r.Title, r.Url, ImageWidth1, ImageHeight1);

                    ltrTopItem.Text = string.Format(
                        TopItemFormat,
                        r.Url,
                        r.Title,
                        r.EncodedTitle,
                        string.IsNullOrEmpty(r.InitSapo) ? string.Empty : string.Format("<p>{0}</p>", r.InitSapo),
                        avatar,
                        string.Empty
                        );
                }
                #endregion

                #region OtherItems
                {
                    if (data.Length > 1)
                    {
                        var sb = new System.Text.StringBuilder();
                        for (var i = 1; i < data.Length; i++)
                        {
                            var r = data[i];
                            var avatar = PresentationUtils.Photo.BuildPhoto(r.Avatar, r.Title, r.Url, ImageWidth2, ImageHeight2);

                            sb.AppendFormat(
                                LiFormat,
                                r.Url,
                        r.Title,
                        r.EncodedTitle,
                                avatar,
                                string.Empty);
                        }
                        ltrOtherItem.Text = sb.ToString();
                    }
                }
                #endregion

                return true;
            }
            return false;
        }
    }
}